package com.gelapps.memogel.credentials;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.gelapps.memogel.Constants;

public class CredentialXMLHandler extends DefaultHandler{
	
	 Boolean currentElement = false;
	    String currentValue = "";
	    CredentialMaster item = null;
	    private ArrayList<CredentialMaster> itemsList = new ArrayList<CredentialMaster>();
	 
	    public ArrayList<CredentialMaster> getItemsList() {
	        return itemsList;
	    }
	 
	    // Called when tag starts 
	     @Override
	    public void startElement(String uri, String localName, String qName,
	            Attributes attributes) throws SAXException {
	 
	        currentElement = true;
	        currentValue = "";
	        if (localName.equals("dc")) {
	            item = new CredentialMaster();
	        } 
	 
	    }
	 
	    // Called when tag closing 
	    @Override
	     public void endElement(String uri, String localName, String qName)
	    		    throws SAXException {
	 
	        currentElement = false;
	 
	        /** set value */
	        if (localName.equalsIgnoreCase(Constants.getCredentialsName()))               item.setName(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsUser()))          item.setUser(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsPass()))          item.setPass(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsDateCreated()))   item.setDateCreated(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsExpiry()))        item.setDataExpiry(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsDateLastUsed()))  item.setDateLastUsed(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsSite()))          item.setSite(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsNote()))          item.setNote(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCredentialsElement()))       itemsList.add(item);
	    }
	 
	    // Called to get tag characters 
	    @Override
	    public void characters(char[] ch, int start, int length)
	    throws SAXException {
	 
	        if (currentElement) {
	            currentValue = currentValue +  new String(ch, start, length);
	        }
	 
	    }

}
